package com.ruoyi.model;


import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@Data
@NoArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class LoginMember implements UserDetails {
    private Member member;
    private List<String> permissions;

    public LoginMember(Member member, List<String> permissions){
        this.member=member;
        this.permissions=permissions;
    }

    @JsonIgnore
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> authorities=new ArrayList<>();
//        for (String permission : permissions) {
//            SimpleGrantedAuthority authority=new SimpleGrantedAuthority(permission);
//            authorities.add(authority);
//        }
        authorities=permissions.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList());
        return authorities;
    }

    @Override
    public String getPassword() {
        return member.getPassword();
    }

    @Override
    public String getUsername() {
        return member.getTel();
    }



    //账户没有过期（true没有过期 false过期）
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    //账户没有被锁定 （true没被锁 false：锁定）
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    //密码没有过期 true没过期 false过期
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    //当前用户是否可用 true可用 false不可用
    @Override
    public boolean isEnabled() {
        return true;
    }
}
